﻿

using System.Text.Json;

try
{
    string sourceDirectory = string.Empty;
    do
    {
        Console.WriteLine("请输入源文件夹:");
        sourceDirectory = Console.ReadLine() ?? string.Empty;
    }
    while (!Directory.Exists(sourceDirectory));

    string targetDirectory = string.Empty;
    do
    {
        Console.WriteLine("请输入目标文件夹:");
        targetDirectory = Console.ReadLine() ?? string.Empty;
    }
    while (!Directory.Exists(targetDirectory));

    var filesCount = Directory.GetDirectories(sourceDirectory).Length;
    var filesCountRest = filesCount;
    Console.WriteLine($"正在整理并创建视频文件...共计 {filesCount} 个");

    foreach (var dirPath in Directory.GetDirectories(sourceDirectory))
    {
        var jsonFilePath = Directory.GetFiles(dirPath).Where(c => c.EndsWith(".info", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        if (string.IsNullOrWhiteSpace(jsonFilePath))
            continue;
        JsonDocument json = JsonDocument.Parse(File.ReadAllText(jsonFilePath));
        var fileName = "P"+json.RootElement.GetProperty("PartNo").Deserialize<string>()?.PadLeft(2,'0') +" "+ json.RootElement.GetProperty("PartName").Deserialize<string>();

        var videoFilePath = Directory.GetFiles(dirPath).Where(c => c.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
        if (string.IsNullOrWhiteSpace(videoFilePath))
            continue;
        var tagetFileName = $@"{targetDirectory}\{fileName}.mp4";
        //设置界面进度状态
        Console.WriteLine($"还有 {filesCountRest--} 个,当前正在生成文件:{tagetFileName}");
        //复制视频文件
        File.Copy(videoFilePath, tagetFileName, true);
    }
    Console.WriteLine($"整理视频文件完成...共计 {filesCount} 个");
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}